﻿using EV.EventBus;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZoneLine.Common;
using ZoneLine.Stations;
using ZoneLine.ViewModels;
using ZoneLine.Views;

namespace ZoneLine
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            var mbox = HandyControl.Controls.MessageBox.Ask("确定是否关闭MES系统软件？", "温馨提示");
            if (mbox != MessageBoxResult.OK)
            {
                e.Cancel = true;
            }
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            MessageCenter.Subscribe(nameof(QOP006Station) + "uilog", nameof(MainWindow), (s) =>
            {
                Op006ShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });

            MessageCenter.Subscribe(nameof(QOP010Station) + "uilog", nameof(MainWindow), (s) =>
            {
                Op010ShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });

            MessageCenter.Subscribe(nameof(QOP020Station) + "uilog", nameof(MainWindow), (s) =>
            {
                Op020ShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });

            MessageCenter.Subscribe(nameof(QOP050LStation) + "uilog", nameof(MainWindow), (s) =>
            {
                Op050LShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });

            MessageCenter.Subscribe(nameof(QOP050RStation) + "uilog", nameof(MainWindow), (s) =>
            {
                Op050RShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });
        }

        private int _op006Count = 0;

        private void Op006ShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _op006Count++;
                    if (_op006Count > 500)
                    {
                        OP006Log.Document.Blocks.Clear();
                        _op006Count = 0;
                    }
                    OP006Log.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    OP006Log.ScrollToEnd();
                });
            }
        }

        private int _op010Count = 0;

        private void Op010ShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _op010Count++;
                    if (_op010Count > 500)
                    {
                        OP010Log.Document.Blocks.Clear();
                        _op010Count = 0;
                    }
                    OP010Log.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    OP010Log.ScrollToEnd();
                });
            }
        }

        private int _op020Count = 0;

        private void Op020ShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _op020Count++;
                    if (_op020Count > 500)
                    {
                        OP020Log.Document.Blocks.Clear();
                        _op020Count = 0;
                    }
                    OP020Log.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    OP020Log.ScrollToEnd();
                });
            }
        }

        private int _op050LCount = 0;

        private void Op050LShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _op050LCount++;
                    if (_op050LCount > 500)
                    {
                        OP050LLog.Document.Blocks.Clear();
                        _op050LCount = 0;
                    }
                    OP050LLog.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    OP050LLog.ScrollToEnd();
                });
            }
        }

        private int _op050RCount = 0;

        private void Op050RShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _op050RCount++;
                    if (_op050RCount > 500)
                    {
                        OP050RLog.Document.Blocks.Clear();
                        _op050RCount = 0;
                    }
                    OP050RLog.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    OP050RLog.ScrollToEnd();
                });
            }
        }

        private void OpenHistoryView(object sender, RoutedEventArgs e)
        {
            var view = new HistoryData();
            view.Owner = this;
            view.Show();
        }

        private void BtnOpenConfigView(object sender, RoutedEventArgs e)
        {
            var view = new StationConfigView();
            view.Owner = this;
            view.ShowDialog();
        }

        private void BtnPrinter(object sender, RoutedEventArgs e)
        {
            try
            {
                var demoPath = "C:\\Users\\Mr\\source\\repos\\PrintHelper\\TestClient_BartenderAlone\\bin\\Debug\\BC_DEFAULT.btw";
                var paras = new Dictionary<string, string>();
                paras.Add("superd", "helloworld");
                PrinterHelper.PrinterLabel(demoPath, "Microsoft Print to PDF", paras);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("打印失败" + ex.Message, "温馨提示");
            }
        }

        private void BtnPrintPreview(object sender, RoutedEventArgs e)
        {
            try
            {
                var demoPath = "C:\\Users\\Mr\\source\\repos\\PrintHelper\\TestClient_BartenderAlone\\bin\\Debug\\BC_DEFAULT.btw";
                var paras = new Dictionary<string, string>();
                paras.Add("superd", "helloworld");
                PrinterHelper.PrinterLabelShow(demoPath, "Microsoft Print to PDF", paras);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("打印失败" + ex.Message, "温馨提示");
            }
        }

        private void QueryStationResultView(object sender, RoutedEventArgs e)
        {
            var view = StationResultView.GetView();
            view.Show();
        }

        private void PePieceReplaceView(object sender, RoutedEventArgs e)
        {
            var view = PackageReplaceView.GetView();
            view.Show();
            view.Activate();
        }

        private void IndexView(object sender, RoutedEventArgs e)
        {
            var view = new PrintIndexView();
            view.Topmost = true;
            view.Show();
        }

        private void OpenAuth(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is MainViewModel vm)
            {
                var view = new AuthView(() => { vm.EnterAuth = true; });
                view.Topmost = true;
                view.Show();
            }
        }

        private void CloseAuth(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is MainViewModel vm)
            {
                vm.EnterAuth = false;
            }
        }

        private void ClearDataBtn(object sender, RoutedEventArgs e)
        {
            try
            {
                var res = HandyControl.Controls.MessageBox.Ask("此操作不可逆，确认清除今日生产所产生的所有数据吗？", "温馨提示");
                if (res == MessageBoxResult.OK)
                {
                    InitHelper.DeleteTodayDatas();
                    HandyControl.Controls.MessageBox.Success("今日数据清除完成！", "温馨提示");
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error("删除数据异常" + ex.Message, "温馨提示");
            }
        }

        private void WorkSituationView(object sender, RoutedEventArgs e)
        {
            AuthView authView = new AuthView(() => {
                WorkSituationView workSituationView = new WorkSituationView();
                workSituationView.Show();
            });
            authView.ShowDialog();
        }
    }
}